<template>
  <div class="login-view">
    <div class="login_form">
      <div class="title">
        <img src="@/assets/img/z-mall-logo@2x.png" alt="" />
        <p>ZMALL商城管理系统</p>
      </div>
      <el-form
        class="login-container"
        :rules="rules"
        :model="ruleForm"
        ref="ruleForm"
      >
        <el-form-item prop="nickname">
          <el-input
            clearable
            prefix-icon="el-icon-user"
            type="text"
            placeholder="请输入用户名"
            v-model="ruleForm.nickname"
          ></el-input>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            clearable
            prefix-icon="el-icon-user"
            type="text"
            v-model="ruleForm.username"
            placeholder="请输入真实姓名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            show-password
            placeholder="请输入密码"
            v-model="ruleForm.password"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            show-password
            placeholder="请重复输入密码"
            v-model="ruleForm.checkPass"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <router-link to="/login">回到登录</router-link>
          <el-button type="primary" style="width: 100%" @click="submitForm()"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { register } from "@/api/index";
// import { get } from "@/utils/store";

export default {
  name: "RegisterView",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        nickname: "",
        password: "",
        checkPass: "",
      },
      rules: {
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        username: [
          {
            required: true,
            message: "请输入账号",
            trigger: "blur",
          },
          {
            min: 3,
            max: 8,
            message: "长度在 3 到 8 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            validator: validatePass,
            trigger: "blur",
          },
        ],
        checkPass: [
          {
            validator: validatePass2,
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          try {
            register({
              nickname: this.ruleForm.nickname,
              username: this.ruleForm.username,
              password: this.ruleForm.password,
            });
            this.$router.push("/login");
            this.$message({
              message: "注册成功",
              type: "success",
            });
          } catch (err) {
            this.$message.error(err);
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.login-view {
  width: 100vw;
  background-size: cover;
  background-position: center;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: url("~@/assets/img/login-bg.png");
  .title {
    padding-top: 25px;
    vertical-align: middle;
    display: flex;
    margin-left: 20%;
    > img {
      vertical-align: middle;
      margin-right: 5px;
      width: 50px;
      height: 50px;
    }
  }
  .login_form {
    width: 400px;
    height: 450px;
    background-color: rgba($color: #fff, $alpha: 0.4);
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .title_register {
      padding-top: 25px;
      vertical-align: middle;
      display: flex;
      margin-left: 30%;
      > p {
        font-size: 20px;
        color: white;
        font-family: "微软雅黑";
      }
    }
  }
}
.el-form {
  padding: 20px;
}
a {
  margin-left: 153px;
  text-decoration: none;
}
.el-form-item {
  margin-top: 15px;
}
</style>
